﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using BALayer;

namespace PGBT
{
    public partial class QuanLyNguoiDung : Form
    {
        private NguoiDung dbND = null;
        private DataSet ds = null;

        private bool flag = false;

        public QuanLyNguoiDung()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            dbND = new NguoiDung();
            ds = dbND.DanhSachNguoiDung();
            dgvNguoiDung.AutoGenerateColumns = false;
            dgvNguoiDung.DataSource = ds.Tables[0];
            dgvNguoiDung.AutoResizeColumns();
        }

        public void ClearInput()
        {
            txtID.Clear();
            txtUsername.Clear();
            txtEmail.Clear();
            txtSDT.Clear();
        }

        private void QuanLyNguoiDung_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            flag = true;

            btnSave.Enabled = true;
            btnAdd.Enabled = false;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;

            txtUsername.ReadOnly = false;
            txtEmail.ReadOnly = false;
            txtSDT.ReadOnly = false;

            ClearInput();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string error = "";

            if(!dbND.XoaNguoiDung(ref error, txtID.Text.ToString()))
            {
                MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            ClearInput();

            LoadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
            btnAdd.Enabled = false;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;

            txtUsername.ReadOnly = false;
            txtEmail.ReadOnly = false;
            txtSDT.ReadOnly = false;

            flag = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string error = "";

            if (flag)
            {
                int mand = dbND.ThemNguoiDung(ref error, txtUsername.Text.ToString(), txtUsername.Text.ToString(), txtEmail.Text.ToString(), txtSDT.Text.ToString());

                if(mand == -1)
                {
                    MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                if (!dbND.CapNhatNguoiDung(ref error, Convert.ToInt32(txtID.Text.ToString()), txtUsername.Text.ToString(),
                    txtEmail.Text.ToString(), txtSDT.Text.ToString()))
                {
                    MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            LoadData();

            ClearInput();

            btnSave.Enabled = false;
            btnAdd.Enabled = true;
            btnDel.Enabled = true;
            btnEdit.Enabled = true;

            txtUsername.ReadOnly = true;
            txtEmail.ReadOnly = true;
            txtSDT.ReadOnly = true;
        }

        private void dgvNguoiDung_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvNguoiDung.CurrentCell.RowIndex;
            txtID.Text = ds.Tables[0].Rows[index].ItemArray[0].ToString();
            txtUsername.Text = ds.Tables[0].Rows[index].ItemArray[1].ToString();
            txtEmail.Text = ds.Tables[0].Rows[index].ItemArray[3].ToString();
            txtSDT.Text = ds.Tables[0].Rows[index].ItemArray[4].ToString();
        }
    }
}
